# coding: UTF-8

import time
import config
import base_func
import topo_data

tunnel_info_set = []

def normal_tunnel_test() :
    topo_data.set_resource_location(config.global_local_unit_test_topo)

    # 初始化网络拓扑环境
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_unit_test_topo
    base_func.init_topology_data(topo_input_params)

    # 进行两组测试
    # 第一组测试：B->G [带宽限制： 200]
    tunnel_info1 = {}
    tunnel_info1['name'] = 'B-G-T1'
    tunnel_info1['priority'] = 'gold'
    tunnel_info1['src-ip'] = '192.168.1.253'
    tunnel_info1['dst-ip'] = '192.168.1.248'
    tunnel_info1['bandwidth-demand'] = '100'
    tunnel_info1['delay-demand'] = '4096'
    tunnel_info1['hop-demand'] = '256'
    tunnel_info1['pass-nodes'] = []
    tunnel_info1['avoid-nodes'] = []
    tunnel_info1['pass-links'] = []
    tunnel_info1['avoid-links'] = []
    tunnel_info1['pass-sequence'] = []

    tunnel_id1 = base_func.create_tunnel_info(tunnel_info1)
    tunnel_info_set.append((tunnel_id1, tunnel_info1))

    # 第二组测试：A->F [带宽限制：200]
    tunnel_info2 = {}
    tunnel_info2['name'] = 'A-F-T1'
    tunnel_info2['priority'] = 'gold'
    tunnel_info2['src-ip'] = '192.168.1.254'
    tunnel_info2['dst-ip'] = '192.168.1.249'
    tunnel_info2['bandwidth-demand'] = '100'
    tunnel_info2['delay-demand'] = '4096'
    tunnel_info2['hop-demand'] = '256'
    tunnel_info2['pass-nodes'] = []
    tunnel_info2['avoid-nodes'] = []
    tunnel_info2['pass-links'] = []
    tunnel_info2['avoid-links'] = []
    tunnel_info2['pass-sequence'] = []

    tunnel_id2 = base_func.create_tunnel_info(tunnel_info2)
    tunnel_info_set.append((tunnel_id2, tunnel_info2))

def pass_node_tunnel_test() :
    topo_data.set_resource_location(config.global_local_unit_test_topo)

    # 初始化网络拓扑
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_unit_test_topo
    base_func.init_topology_data(topo_input_params)

    # 第一组测试：B->G [带宽限制： 200]
    tunnel_info1 = {}
    tunnel_info1['name'] = 'B-G-T2'
    tunnel_info1['priority'] = 'gold'
    tunnel_info1['src-ip'] = '192.168.1.253'
    tunnel_info1['dst-ip'] = '192.168.1.248'
    tunnel_info1['bandwidth-demand'] = '100'
    tunnel_info1['delay-demand'] = '4096'
    tunnel_info1['hop-demand'] = '256'

    pass_nodes = []
    pass_links = []
    avoid_nodes = []
    avoid_links = []
    pass_sequence = ''

    # 添加必经节点D
    pass_nodes.append(topo_data.get_node_by_ip('192.168.1.251'))
    pass_sequence += 'N'

    tunnel_info1['pass-nodes'] = pass_nodes
    tunnel_info1['avoid-nodes'] = avoid_nodes
    tunnel_info1['pass-links'] = pass_links
    tunnel_info1['avoid-links'] = avoid_links
    tunnel_info1['pass-sequence'] = pass_sequence

    tunnel_id1 = base_func.create_tunnel_info(tunnel_info1)
    tunnel_info_set.append((tunnel_id1, tunnel_info1))

    # 第二组测试：H->L [带宽限制：200]
    tunnel_info2 = {}
    tunnel_info2['name'] = 'H-L-T1'
    tunnel_info2['priority'] = 'gold'
    tunnel_info2['src-ip'] = '192.168.1.247'
    tunnel_info2['dst-ip'] = '192.168.1.243'
    tunnel_info2['bandwidth-demand'] = '100'
    tunnel_info2['delay-demand'] = '4096'
    tunnel_info2['hop-demand'] = '256'

    pass_nodes = []
    pass_links = []
    avoid_nodes = []
    avoid_links = []
    pass_sequence = ''

    # 添加必经节点J
    pass_nodes.append(topo_data.get_node_by_ip('192.168.1.245'))
    pass_sequence += 'N'

    tunnel_info2['pass-nodes'] = pass_nodes
    tunnel_info2['avoid-nodes'] = avoid_nodes
    tunnel_info2['pass-links'] = pass_links
    tunnel_info2['avoid-links'] = avoid_links
    tunnel_info2['pass-sequence'] = pass_sequence

    tunnel_id2 = base_func.create_tunnel_info(tunnel_info2)
    tunnel_info_set.append((tunnel_id2, tunnel_info2))

def pass_link_tunnel_test() :
    topo_data.set_resource_location(config.global_local_unit_test_topo)

    # 初始化网络拓扑
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_unit_test_topo
    base_func.init_topology_data(topo_input_params)

    # 第一组测试：D->G [带宽限制： 200]
    tunnel_info1 = {}
    tunnel_info1['name'] = 'D-G-T1'
    tunnel_info1['priority'] = 'gold'
    tunnel_info1['src-ip'] = '192.168.1.251'
    tunnel_info1['dst-ip'] = '192.168.1.248'
    tunnel_info1['bandwidth-demand'] = '100'
    tunnel_info1['delay-demand'] = '4096'
    tunnel_info1['hop-demand'] = '256'

    pass_nodes = []
    pass_links = []
    avoid_nodes = []
    avoid_links = []
    pass_sequence = ''

    # 添加必经链路E-G
    pass_links.append(topo_data.get_link_by_name('E-G'))
    pass_sequence += 'L'

    tunnel_info1['pass-nodes'] = pass_nodes
    tunnel_info1['avoid-nodes'] = avoid_nodes
    tunnel_info1['pass-links'] = pass_links
    tunnel_info1['avoid-links'] = avoid_links
    tunnel_info1['pass-sequence'] = pass_sequence

    tunnel_id1 = base_func.create_tunnel_info(tunnel_info1)
    tunnel_info_set.append((tunnel_id1, tunnel_info1))

    # 第二组测试：H->L [带宽限制：200]
    tunnel_info2 = {}
    tunnel_info2['name'] = 'H-L-T2'
    tunnel_info2['priority'] = 'gold'
    tunnel_info2['src-ip'] = '192.168.1.247'
    tunnel_info2['dst-ip'] = '192.168.1.243'
    tunnel_info2['bandwidth-demand'] = '100'
    tunnel_info2['delay-demand'] = '4096'
    tunnel_info2['hop-demand'] = '256'

    pass_nodes = []
    pass_links = []
    avoid_nodes = []
    avoid_links = []
    pass_sequence = ''

    # 添加必经链路J-K
    pass_links.append(topo_data.get_link_by_name('J-K'))
    pass_sequence += 'L'

    tunnel_info2['pass-nodes'] = pass_nodes
    tunnel_info2['avoid-nodes'] = avoid_nodes
    tunnel_info2['pass-links'] = pass_links
    tunnel_info2['avoid-links'] = avoid_links
    tunnel_info2['pass-sequence'] = pass_sequence

    tunnel_id2 = base_func.create_tunnel_info(tunnel_info2)
    tunnel_info_set.append((tunnel_id2, tunnel_info2))

def pass_node_link_tunnel_test() :
    topo_data.set_resource_location(config.global_local_unit_test_topo)

    # 初始化网络拓扑
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_unit_test_topo
    base_func.init_topology_data(topo_input_params)

    # 第一组测试：D->L [带宽限制： 200]
    tunnel_info1 = {}
    tunnel_info1['name'] = 'D-L-T2'
    tunnel_info1['priority'] = 'gold'
    tunnel_info1['src-ip'] = '192.168.1.251'
    tunnel_info1['dst-ip'] = '192.168.1.243'
    tunnel_info1['bandwidth-demand'] = '100'
    tunnel_info1['delay-demand'] = '4096'
    tunnel_info1['hop-demand'] = '256'

    pass_nodes = []
    pass_links = []
    avoid_nodes = []
    avoid_links = []
    pass_sequence = ''

    # 添加必经节点E
    pass_nodes.append(topo_data.get_node_by_ip('192.168.1.250'))
    pass_sequence += 'N'

    # 添加必经链路J-K
    pass_links.append(topo_data.get_link_by_name('J-K'))
    pass_sequence += 'L'

    tunnel_info1['pass-nodes'] = pass_nodes
    tunnel_info1['avoid-nodes'] = avoid_nodes
    tunnel_info1['pass-links'] = pass_links
    tunnel_info1['avoid-links'] = avoid_links
    tunnel_info1['pass-sequence'] = pass_sequence

    print tunnel_info1

    tunnel_id1 = base_func.create_tunnel_info(tunnel_info1)
    tunnel_info_set.append((tunnel_id1, tunnel_info1))

def cspf_tunnel_test() :
    topo_data.set_resource_location(config.global_local_unit_test_topo)

    # 初始化网络拓扑
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_unit_test_topo
    base_func.init_topology_data(topo_input_params)

    # 预备工作：B->D [带宽限制： 400]
    tunnel_info1 = {}
    tunnel_info1['name'] = 'D-G-T1'
    tunnel_info1['priority'] = 'gold'
    tunnel_info1['src-ip'] = '192.168.1.253'
    tunnel_info1['dst-ip'] = '192.168.1.251'
    tunnel_info1['bandwidth-demand'] = '400'
    tunnel_info1['delay-demand'] = '4096'
    tunnel_info1['hop-demand'] = '256'
    tunnel_info1['pass-nodes'] = []
    tunnel_info1['avoid-nodes'] = []
    tunnel_info1['pass-links'] = []
    tunnel_info1['avoid-links'] = []
    tunnel_info1['pass-sequence'] = []

    tunnel_id1 = base_func.create_tunnel_info(tunnel_info1)
    tunnel_info_set.append((tunnel_id1, tunnel_info1))

    # 核心测试：B->F [带宽限制：820]
    tunnel_info2 = {}
    tunnel_info2['name'] = 'A-F-T2'
    tunnel_info2['priority'] = 'gold'
    tunnel_info2['src-ip'] = '192.168.1.253'
    tunnel_info2['dst-ip'] = '192.168.1.249'
    tunnel_info2['bandwidth-demand'] = '820'
    tunnel_info2['delay-demand'] = '4096'
    tunnel_info2['hop-demand'] = '256'
    tunnel_info2['pass-nodes'] = []
    tunnel_info2['avoid-nodes'] = []
    tunnel_info2['pass-links'] = []
    tunnel_info2['avoid-links'] = []
    tunnel_info2['pass-sequence'] = []

    tunnel_id2 = base_func.create_tunnel_info(tunnel_info2)
    tunnel_info_set.append((tunnel_id2, tunnel_info2))

def preempt_tunnel_test() :
    topo_data.set_resource_location(config.global_local_unit_test_topo)

    # 初始化网络拓扑
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_unit_test_topo
    base_func.init_topology_data(topo_input_params)

    # 预备工作：A->G [带宽限制： 400]
    tunnel_info1 = {}
    tunnel_info1['name'] = 'A-G-T2'
    tunnel_info1['priority'] = 'gold'
    tunnel_info1['src-ip'] = '192.168.1.254'
    tunnel_info1['dst-ip'] = '192.168.1.248'
    tunnel_info1['bandwidth-demand'] = '400'
    tunnel_info1['delay-demand'] = '4096'
    tunnel_info1['hop-demand'] = '256'
    tunnel_info1['pass-nodes'] = []
    tunnel_info1['avoid-nodes'] = []
    tunnel_info1['pass-links'] = []
    tunnel_info1['avoid-links'] = []
    tunnel_info1['pass-sequence'] = []

    tunnel_id1 = base_func.create_tunnel_info(tunnel_info1)
    tunnel_info_set.append((tunnel_id1, tunnel_info1))

    # 预备工作：A->E [带宽限制：300]
    tunnel_info2 = {}
    tunnel_info2['name'] = 'A-E-T2'
    tunnel_info2['priority'] = 'silver'
    tunnel_info2['src-ip'] = '192.168.1.254'
    tunnel_info2['dst-ip'] = '192.168.1.250'
    tunnel_info2['bandwidth-demand'] = '300'
    tunnel_info2['delay-demand'] = '4096'
    tunnel_info2['hop-demand'] = '256'
    tunnel_info2['pass-nodes'] = []
    tunnel_info2['avoid-nodes'] = []
    tunnel_info2['pass-links'] = []
    tunnel_info2['avoid-links'] = []
    tunnel_info2['pass-sequence'] = []

    tunnel_id2 = base_func.create_tunnel_info(tunnel_info2)
    tunnel_info_set.append((tunnel_id2, tunnel_info2))

    # 核心过程：B->F [带宽限制：400]
    tunnel_info3 = {}
    tunnel_info3['name'] = 'B-F-T3'
    tunnel_info3['priority'] = 'gold'
    tunnel_info3['src-ip'] = '192.168.1.253'
    tunnel_info3['dst-ip'] = '192.168.1.249'
    tunnel_info3['bandwidth-demand'] = '400'
    tunnel_info3['delay-demand'] = '4096'
    tunnel_info3['hop-demand'] = '256'
    tunnel_info3['pass-nodes'] = []
    tunnel_info3['avoid-nodes'] = []
    tunnel_info3['pass-links'] = []
    tunnel_info3['avoid-links'] = []
    tunnel_info3['pass-sequence'] = []

    tunnel_id3 = base_func.create_tunnel_info(tunnel_info3)
    tunnel_info_set.append((tunnel_id2, tunnel_info3))

def node_fault_recovery() :
    topo_data.set_resource_location(config.global_local_unit_test_topo)

    # 初始化网络拓扑
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_unit_test_topo
    base_func.init_topology_data(topo_input_params)

    # 预备工作：B->G [带宽限制： 100]
    tunnel_info1 = {}
    tunnel_info1['name'] = 'B-G-T4'
    tunnel_info1['priority'] = 'gold'
    tunnel_info1['src-ip'] = '192.168.1.253'
    tunnel_info1['dst-ip'] = '192.168.1.248'
    tunnel_info1['bandwidth-demand'] = '100'
    tunnel_info1['delay-demand'] = '4096'
    tunnel_info1['hop-demand'] = '256'
    tunnel_info1['pass-nodes'] = []
    tunnel_info1['avoid-nodes'] = []
    tunnel_info1['pass-links'] = []
    tunnel_info1['avoid-links'] = []
    tunnel_info1['pass-sequence'] = []

    tunnel_id1 = base_func.create_tunnel_info(tunnel_info1)
    tunnel_info_set.append((tunnel_id1, tunnel_info1))

    # 预备工作：A->B [带宽限制：100]
    tunnel_info2 = {}
    tunnel_info2['name'] = 'A-B-T2'
    tunnel_info2['priority'] = 'silver'
    tunnel_info2['src-ip'] = '192.168.1.254'
    tunnel_info2['dst-ip'] = '192.168.1.253'
    tunnel_info2['bandwidth-demand'] = '100'
    tunnel_info2['delay-demand'] = '4096'
    tunnel_info2['hop-demand'] = '256'
    tunnel_info2['pass-nodes'] = []
    tunnel_info2['avoid-nodes'] = []
    tunnel_info2['pass-links'] = []
    tunnel_info2['avoid-links'] = []
    tunnel_info2['pass-sequence'] = []

    tunnel_id2 = base_func.create_tunnel_info(tunnel_info2)
    tunnel_info_set.append((tunnel_id2, tunnel_info2))

    # 准备工作：D->F [带宽限制：100]
    tunnel_info3 = {}
    tunnel_info3['name'] = 'D-F-T3'
    tunnel_info3['priority'] = 'gold'
    tunnel_info3['src-ip'] = '192.168.1.251'
    tunnel_info3['dst-ip'] = '192.168.1.249'
    tunnel_info3['bandwidth-demand'] = '100'
    tunnel_info3['delay-demand'] = '4096'
    tunnel_info3['hop-demand'] = '256'
    tunnel_info3['pass-nodes'] = []
    tunnel_info3['avoid-nodes'] = []
    tunnel_info3['pass-links'] = []
    tunnel_info3['avoid-links'] = []
    tunnel_info3['pass-sequence'] = []

    tunnel_id3 = base_func.create_tunnel_info(tunnel_info3)
    tunnel_info_set.append((tunnel_id2, tunnel_info3))

    # 准备工作：F->G [带宽限制：100]
    tunnel_info4 = {}
    tunnel_info4['name'] = 'F-G-T1'
    tunnel_info4['priority'] = 'gold'
    tunnel_info4['src-ip'] = '192.168.1.249'
    tunnel_info4['dst-ip'] = '192.168.1.248'
    tunnel_info4['bandwidth-demand'] = '100'
    tunnel_info4['delay-demand'] = '4096'
    tunnel_info4['hop-demand'] = '256'
    tunnel_info4['pass-nodes'] = []
    tunnel_info4['avoid-nodes'] = []
    tunnel_info4['pass-links'] = []
    tunnel_info4['avoid-links'] = []
    tunnel_info4['pass-sequence'] = []

    tunnel_id4 = base_func.create_tunnel_info(tunnel_info4)
    tunnel_info_set.append((tunnel_id2, tunnel_info4))

    # 节点故障 NODE[F]
    falut_node_list = []
    falut_node_list.append(topo_data.get_node_by_ip("192.168.1.249"))
    falut_link_list = []
    fault_data = {}

    fault_data['fault-nodes'] = falut_node_list
    fault_data['fault-links'] = falut_link_list

    base_func.execute_fault_recovery(fault_data)

def link_fault_recovery() :
    topo_data.set_resource_location(config.global_local_unit_test_topo)

    # 初始化网络拓扑
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_unit_test_topo
    base_func.init_topology_data(topo_input_params)

    # 预备工作：B->G [带宽限制： 100]
    tunnel_info1 = {}
    tunnel_info1['name'] = 'B-G-T4'
    tunnel_info1['priority'] = 'gold'
    tunnel_info1['src-ip'] = '192.168.1.253'
    tunnel_info1['dst-ip'] = '192.168.1.248'
    tunnel_info1['bandwidth-demand'] = '100'
    tunnel_info1['delay-demand'] = '4096'
    tunnel_info1['hop-demand'] = '256'
    tunnel_info1['pass-nodes'] = []
    tunnel_info1['avoid-nodes'] = []
    tunnel_info1['pass-links'] = []
    tunnel_info1['avoid-links'] = []
    tunnel_info1['pass-sequence'] = []

    tunnel_id1 = base_func.create_tunnel_info(tunnel_info1)
    tunnel_info_set.append((tunnel_id1, tunnel_info1))

    # 预备工作：A->B [带宽限制：100]
    tunnel_info2 = {}
    tunnel_info2['name'] = 'A-B-T2'
    tunnel_info2['priority'] = 'silver'
    tunnel_info2['src-ip'] = '192.168.1.254'
    tunnel_info2['dst-ip'] = '192.168.1.253'
    tunnel_info2['bandwidth-demand'] = '100'
    tunnel_info2['delay-demand'] = '4096'
    tunnel_info2['hop-demand'] = '256'
    tunnel_info2['pass-nodes'] = []
    tunnel_info2['avoid-nodes'] = []
    tunnel_info2['pass-links'] = []
    tunnel_info2['avoid-links'] = []
    tunnel_info2['pass-sequence'] = []

    tunnel_id2 = base_func.create_tunnel_info(tunnel_info2)
    tunnel_info_set.append((tunnel_id2, tunnel_info2))

    # 准备工作：D->F [带宽限制：100]
    tunnel_info3 = {}
    tunnel_info3['name'] = 'D-F-T3'
    tunnel_info3['priority'] = 'gold'
    tunnel_info3['src-ip'] = '192.168.1.251'
    tunnel_info3['dst-ip'] = '192.168.1.249'
    tunnel_info3['bandwidth-demand'] = '100'
    tunnel_info3['delay-demand'] = '4096'
    tunnel_info3['hop-demand'] = '256'
    tunnel_info3['pass-nodes'] = []
    tunnel_info3['avoid-nodes'] = []
    tunnel_info3['pass-links'] = []
    tunnel_info3['avoid-links'] = []
    tunnel_info3['pass-sequence'] = []

    tunnel_id3 = base_func.create_tunnel_info(tunnel_info3)
    tunnel_info_set.append((tunnel_id2, tunnel_info3))

    # 链路故障 [C-F]
    falut_node_list = []
    falut_link_list = []
    falut_link_list.append(topo_data.get_link_by_name('C-F'))
    fault_data = {}
    fault_data['fault-nodes'] = falut_node_list
    fault_data['fault-links'] = falut_link_list

    base_func.execute_fault_recovery(fault_data)


def load_balance_test() :
    topo_data.set_resource_location(config.global_local_unit_test_topo)

    # 初始化网络拓扑
    topo_input_params = {}
    topo_input_params['resource-location'] = config.global_remote_unit_test_topo
    base_func.init_topology_data(topo_input_params)

    # 下发配置信息
    config_info = {}
    config_info['load-balance-penalty-factor'] = config.global_load_balance_factor
    config_info['link-quality-penalty-factor'] = config.global_link_quality_factor
    config_info['bandwidth-demand-relax-factor'] = config.global_bandwidth_relax_factor
    base_func.regulate_pce_config(config_info)

    time.sleep(1)

    # 预备工作：C->F [带宽限制： 800]
    tunnel_info1 = {}
    tunnel_info1['name'] = 'C-F-T4'
    tunnel_info1['priority'] = 'gold'
    tunnel_info1['src-ip'] = '192.168.1.252'
    tunnel_info1['dst-ip'] = '192.168.1.249'
    tunnel_info1['bandwidth-demand'] = '800'
    tunnel_info1['delay-demand'] = '4096'
    tunnel_info1['hop-demand'] = '256'
    tunnel_info1['pass-nodes'] = []
    tunnel_info1['avoid-nodes'] = []
    tunnel_info1['pass-links'] = []
    tunnel_info1['avoid-links'] = []
    tunnel_info1['pass-sequence'] = []

    tunnel_id1 = base_func.create_tunnel_info(tunnel_info1)
    tunnel_info_set.append((tunnel_id1, tunnel_info1))

    # 测试隧道：B->F [带宽限制：600]
    tunnel_info2 = {}
    tunnel_info2['name'] = 'B-F-T4'
    tunnel_info2['priority'] = 'gold'
    tunnel_info2['src-ip'] = '192.168.1.253'
    tunnel_info2['dst-ip'] = '192.168.1.249'
    tunnel_info2['bandwidth-demand'] = '600'
    tunnel_info2['delay-demand'] = '4096'
    tunnel_info2['hop-demand'] = '256'
    tunnel_info2['pass-nodes'] = []
    tunnel_info2['avoid-nodes'] = []
    tunnel_info2['pass-links'] = []
    tunnel_info2['avoid-links'] = []
    tunnel_info2['pass-sequence'] = []

    tunnel_id2 = base_func.create_tunnel_info(tunnel_info2)
    tunnel_info_set.append((tunnel_id2, tunnel_info2))